/**
 * 位置对象
 * @property t 发生时间
 * @property lng 经度
 * @property lat 纬度
 * @property s 速度（公里/时）
 */
exports.Position = function (t, lng, lat, s) {
    var _this = this;
    this.t = t;
    this.lng = lng;
    this.lat = lat;
    this.s = s;

    this.getDistinct = function (another) {
        const radius = 6370996.81 //球半径

        if (_this.lng == another.lng && _this.lat == another.lat) {
            return 0;
        } else {
            aLng = _this.lng * Math.PI / 180;
            aLat = _this.lat * Math.PI / 180;
            bLng = another.lng * Math.PI / 180;
            bLat = another.lat * Math.PI / 180;
            return Math.abs(radius * Math.acos(Math.sin(aLat) * Math.sin(bLat) + Math.cos(aLat) * Math.cos(bLat) * Math.cos(bLng - aLng)));
        }
    };
}


exports.GpsSimpleFilter = function () {
    const sl = [5,25] //速度级别区分点
    const kl = [30,100,200] //不同速度级别的每秒距离限制
    
    var _this = this;
    this.basePosition = null;
    this.lastPosition = null;

    /**
     * 重置数据，移除保留的信息
     */
    this.reset = function () {
        _this.basePosition = null;
        _this.lastPosition = null;
    }

    /**
     * 记录新的位置
     * @returns boolean 如果位置有用返回true，如果没用返回false
     */
    this.nextPosition = function (p) {
        if (_this.lastPosition == null) {
            _this.basePosition = p;
            _this.lastPosition = p;
            return true;
        }

        if (_this.basePosition.lng === p.lng &&
            _this.basePosition.lat === p.lat) {
            //_this.basePosition = p;
            _this.lastPosition = p;
            return true;
        }

        if (_this.lastPosition.lng === p.lng &&
            _this.lastPosition.lat === p.lat) {
            _this.lastPosition = p;
            return false;
        }

        _this.lastPosition = p;
        var speed = Math.max(_this.basePosition.s, p.s); 
        var kk = 0;
        if (speed <= sl[0]){ //低速情况
            kk = kl[0];
        } else if(speed > sl[0] && speed <= sl[1]) {
            kk = kl[1];
        } else {
            kk = kl[2];
        }

        var interval = Math.ceil((p.t.getTime() - _this.basePosition.t.getTime())/1000);
        if(interval > 300){
            _this.basePosition = p;
            return true;
        }

        if (p.getDistinct(_this.basePosition) / interval > kk){
            return false;
        }else{
            _this.basePosition = p;
            return true;
        }
    }

};

